package com.screens;

import sheep.game.Sprite;
import sheep.game.State;
import sheep.graphics.Image;
import sheep.gui.TextButton;
import sheep.gui.WidgetAction;
import sheep.gui.WidgetListener;
import android.graphics.Canvas;

import com.bricks2break.Level;
import com.bricks2break.R;
import com.bricks2break.Statics;

/**
 * New screen for Game Over, to show who is winning.
 * @author Hege Beate
 *
 */
public class GameOver extends State implements WidgetListener{

	private String winnerTxt = " ";
	private String loserTxt = " ";
	private TextButton nextLevel;
	private TextButton noNextLvl;
	private TextButton StartMenuButton;
	private TextButton exitGameButton;
	private int lvlNumber;
	private boolean moreLevels = true;
	private String gameoverTxt = Statics.string_game_over_text; 
	
	/**
	 * New screen for Game Over, to show who is winning.
	 * @param winnerTxt, loserTxt, lvlNumber
	 * 
	 */
	public GameOver(String winnerTxt, String loserTxt, int lvlNumber){
		this.lvlNumber = lvlNumber;
		if(winnerTxt == null){
			this.winnerTxt = "There was no winner!";
			this.loserTxt = " ";
		}
		else{
			this.winnerTxt = winnerTxt;
			this.loserTxt = loserTxt;
		}
		nextLevel = new TextButton(Statics.SCREEN_WIDTH*0.3f, Statics.SCREEN_HEIGHT*0.4f, Statics.string_next_level);
		noNextLvl = new TextButton(Statics.SCREEN_WIDTH*0.6f, Statics.SCREEN_HEIGHT*0.4f, Statics.string_no_more_levels);
		StartMenuButton = new TextButton(Statics.SCREEN_WIDTH*0.3f,Statics.SCREEN_HEIGHT*0.6f,Statics.string_start_menu);
		exitGameButton = new TextButton(Statics.SCREEN_WIDTH*0.3f,Statics.SCREEN_HEIGHT*0.8f,Statics.string_exit_game);
		
		addTouchListener(nextLevel);
		addTouchListener(StartMenuButton);
		addTouchListener(exitGameButton);
		
		nextLevel.addWidgetListener(this);
		StartMenuButton.addWidgetListener(this);
		exitGameButton.addWidgetListener(this);
		
	}
	
	/**
	 * Defines what the buttons do.
	 * @param action
	 */
	public void actionPerformed(WidgetAction action) {
		if(action.getSource() == StartMenuButton){
			getGame().popState();
			//getGame().pushState(new StartMenu());
		}
		else if(action.getSource() == exitGameButton){
			getGame().popState();
			System.exit(0);
		}
		else if(action.getSource() == nextLevel){
			if(Statics.bricks.length > lvlNumber+1){
				moreLevels = true;
				getGame().popState();
				getGame().pushState(new Level(lvlNumber+1));
			}
			else{
				moreLevels = false;		
			}
		}
	}
	
	/**
	 * Draw the objects.
	 * @param canvas
	 */
	public void draw(Canvas canvas){
		super.draw(canvas);
		canvas.drawRect(0, 0, Statics.SCREEN_WIDTH, Statics.SCREEN_HEIGHT, Statics.black);		
		canvas.drawText(gameoverTxt, Statics.SCREEN_WIDTH*0.1f, Statics.SCREEN_HEIGHT*0.1f, Statics.txt);
		canvas.drawText(winnerTxt, Statics.SCREEN_WIDTH*0.1f, Statics.SCREEN_HEIGHT*0.2f, Statics.txt);
		canvas.drawText(loserTxt, Statics.SCREEN_WIDTH*0.1f, Statics.SCREEN_HEIGHT*0.25f, Statics.txt);
		
		nextLevel.draw(canvas);
		if(!moreLevels) noNextLvl.draw(canvas);
		StartMenuButton.draw(canvas);
		exitGameButton.draw(canvas);
	}

}
